/*******************************************************************************
 * Copyright (c) 2006 CEA List.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     CEA List - initial API and implementation
 *******************************************************************************/
package com.cea.papyrus.diagram.activity.tabbedproperties.section;

import org.eclipse.swt.SWT;
import org.eclipse.swt.custom.CLabel;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.ui.views.properties.tabbed.ITabbedPropertyConstants;
import org.eclipse.ui.views.properties.tabbed.TabbedPropertySheetPage;
import org.eclipse.uml2.uml.ActivityEdge;

import com.cea.papyrus.core.tabbedproperties.PropertyViewSection;
import com.cea.papyrus.diagraminterchange2.di2.GraphEdge;
import com.cea.papyrus.diagraminterchange2.di2.GraphElement;
import com.cea.papyrus.diagraminterchange2.di2.GraphNode;

/**
 * Manages presentation section of ActivityEdges
 *
 * @author Gilles Cannenterre
 * @since 4 avr. 07
 */
public class ActivityEdgePresentationSection extends PropertyViewSection {

	/** The check box show guard. */
	private Button checkBoxShowGuard;

	/** The current guard appearance. */
	private boolean currentGuardAppearance;
	
	/** The show guard. */
	private SelectionListener showGuard;
	
	/** The check box show weight. */
	private Button checkBoxShowWeight;

	/** The current weight appearance. */
	private boolean currentWeightAppearance;
	
	/** The show weight. */
	private SelectionListener showWeight;
	
	/** The check box show name. */
	private Button checkBoxShowName;
		
	/** The current name appearance. */
	private boolean currentNameAppearance;
	
	/** The show name. */
	private SelectionListener showName;
	
	/**
	 * Creates the controls.
	 * 
	 * @param tabbedPropertySheetPage the tabbed property sheet page
	 * @param parent the parent
	 */
	public void createControls(final Composite parent,
			final TabbedPropertySheetPage tabbedPropertySheetPage) {
		super.createControls(parent, tabbedPropertySheetPage);
		final Composite composite = this.getWidgetFactory().createFlatFormComposite(parent);
		FormData data;
		
		// Show Name
		checkBoxShowName = this.getWidgetFactory().createButton(composite, "Show Name", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(0, 100);
		data.top = new FormAttachment(0, 0);
		checkBoxShowName.setLayoutData(data);
		
		showName = new SelectionListener() {
			/**
			 * Widget default selected.
			 * 
			 * @param e the e
			 */
			public void widgetDefaultSelected(SelectionEvent e){}

			/**
			 * Widget selected.
			 * 
			 * @param e the e
			 */
			public void widgetSelected(SelectionEvent e) {
				if (umlElement instanceof ActivityEdge) {
					if ((diagramElement != null) && (diagramElement instanceof GraphEdge)){
						((GraphNode)((GraphEdge)diagramElement).getContained().get(0)).setIsVisible(!((GraphNode)((GraphEdge)diagramElement).getContained().get(0)).isIsVisible());
						forceToSave();
					}		
				}
			}
		};
		
		checkBoxShowName.addSelectionListener(showName);
		
		// Show Guard
		checkBoxShowGuard = this.getWidgetFactory().createButton(composite, "Show Guard", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(checkBoxShowName, 20);
		data.top = new FormAttachment(0, 0);
		checkBoxShowGuard.setLayoutData(data);
		
		showGuard = new SelectionListener() {
			/**
			 * Widget default selected.
			 * 
			 * @param e the e
			 */
			public void widgetDefaultSelected(SelectionEvent e){}

			/**
			 * Widget selected.
			 * 
			 * @param e the e
			 */
			public void widgetSelected(SelectionEvent e) {
				if (umlElement instanceof ActivityEdge) {
					if ((diagramElement != null) && (diagramElement instanceof GraphEdge)){
						((GraphNode)((GraphEdge)diagramElement).getContained().get(2)).setIsVisible(!((GraphNode)((GraphEdge)diagramElement).getContained().get(2)).isIsVisible());
						forceToSave();
					}		
				}
			}
		};
		
		checkBoxShowGuard.addSelectionListener(showGuard);
				
		// Show Weight
		checkBoxShowWeight = this.getWidgetFactory().createButton(composite, "Show Weight", SWT.CHECK);
		data = new FormData();
		data.left = new FormAttachment(checkBoxShowGuard, 20);
		data.top = new FormAttachment(0, 0);
		checkBoxShowWeight.setLayoutData(data);
		
		showWeight = new SelectionListener() {
			/**
			 * Widget default selected.
			 * 
			 * @param e the e
			 */
			public void widgetDefaultSelected(SelectionEvent e){}

			/**
			 * Widget selected.
			 * 
			 * @param e the e
			 */
			public void widgetSelected(SelectionEvent e) {
				if (umlElement instanceof ActivityEdge) {
					if ((diagramElement != null) && (diagramElement instanceof GraphEdge)){
						((GraphNode)((GraphEdge)diagramElement).getContained().get(3)).setIsVisible(!((GraphNode)((GraphEdge)diagramElement).getContained().get(3)).isIsVisible());
						forceToSave();
					}		
				}
			}
		};
		
		checkBoxShowWeight.addSelectionListener(showWeight);
		
		// Label of the section
		CLabel activityEdgePresentationLabel = this.getWidgetFactory().createCLabel(composite, "Display Options:");
		data = new FormData();
		data.left = new FormAttachment(0, 0);
		data.right = new FormAttachment(checkBoxShowName,
				-ITabbedPropertyConstants.HSPACE);
		data.top = new FormAttachment(checkBoxShowName, 0, SWT.CENTER);
		activityEdgePresentationLabel.setLayoutData(data);
	}

	/**
	 * Refresh the view othe multicplicity in the multicplicityCombo.
	 */
	@Override
	public void refresh() {
		if ((!checkBoxShowName.isDisposed())&&(!checkBoxShowGuard.isDisposed())) {
			// remove all listeners
			checkBoxShowName.removeSelectionListener(showName);
			checkBoxShowGuard.removeSelectionListener(showGuard);
			checkBoxShowWeight.removeSelectionListener(showWeight);
			
			if ((diagramElement != null) && (((GraphElement)diagramElement).getSemanticModel() != null )){
				checkBoxShowName.setEnabled(true);
				checkBoxShowGuard.setEnabled(true);
				checkBoxShowWeight.setEnabled(true);
				
				if (umlElement instanceof ActivityEdge) {
					if (diagramElement instanceof GraphEdge){
						currentNameAppearance = ((GraphNode)((GraphEdge)diagramElement).getContained().get(0)).isIsVisible();
						currentGuardAppearance = ((GraphNode)((GraphEdge)diagramElement).getContained().get(2)).isIsVisible();
						currentWeightAppearance	= ((GraphNode)((GraphEdge)diagramElement).getContained().get(3)).isIsVisible();
					}				
				}
								
			} else {
				checkBoxShowName.setEnabled(false);
				checkBoxShowGuard.setEnabled(false);
				checkBoxShowWeight.setEnabled(false);
			}
			checkBoxShowName.setSelection(currentNameAppearance);
			checkBoxShowGuard.setSelection(currentGuardAppearance);
			checkBoxShowWeight.setSelection(currentWeightAppearance);
			
			checkBoxShowName.addSelectionListener(showName);
			checkBoxShowGuard.addSelectionListener(showGuard);
			checkBoxShowWeight.addSelectionListener(showWeight);
		}		
	}
	
	/*
	 * (non-Javadoc)
	 * @see com.cea.papyrus.core.ui.properties.tabbed.PropertyViewSection#dispose()
	 */
	@Override
	public void dispose(){
		super.dispose();
		if(checkBoxShowName!=null && !checkBoxShowName.isDisposed())
			checkBoxShowName.removeSelectionListener(showName);
		if(checkBoxShowGuard!=null && !checkBoxShowGuard.isDisposed())
			checkBoxShowGuard.removeSelectionListener(showGuard);
		if(checkBoxShowWeight!=null && !checkBoxShowWeight.isDisposed())
			checkBoxShowWeight.removeSelectionListener(showWeight);
	}
}